package xin.marcher.module.lifecycle.service.impl;

import cn.hutool.core.util.StrUtil;
import org.springframework.stereotype.Service;
import xin.marcher.module.common.exception.BaseBizException;
import xin.marcher.module.common.exception.ProductErrorCodeEnum;
import xin.marcher.module.lifecycle.domain.request.ProductExpriDataResultRequest;
import xin.marcher.module.lifecycle.repository.ProductExpriRepository;
import xin.marcher.module.lifecycle.service.ProductExpriDataResultService;

import javax.annotation.Resource;
import java.util.Objects;

/**
 *
 */
@Service
public class ProductExpriDataResultServiceImpl implements ProductExpriDataResultService {

    @Resource
    private ProductExpriRepository productExpriRepository;

    /**
     * 新增考核指标数据
     *
     * @param request
     * @return
     */
    @Override
    public Long saveProductExpriDataResult(ProductExpriDataResultRequest request) {
        checkParams(request);
        return productExpriRepository.saveProductExpriDataResult(request);
    }

    /**
     * 参数检查
     *
     * @param request
     */
    private void checkParams(ProductExpriDataResultRequest request) {
        if (Objects.isNull(request)) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }

        if (Objects.isNull(request.getCategoryId())) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }

        if (StrUtil.isEmpty(request.getItemId())) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }

        if (Objects.isNull(request.getAssessmentType())) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }

        if (Objects.isNull(request.getSalesVolume())) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }

        if (Objects.isNull(request.getSoldPropotion())) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }

        if (Objects.isNull(request.getOrderProfitMargin())) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }

        if (Objects.isNull(request.getReturnRate())) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }

        if (Objects.isNull(request.getComplaintRate())) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }

        if (Objects.isNull(request.getLossRate())) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }
    }
}
